#!/usr/bin/python3
#ct_widget.py

class GridManagement:
    ''' implement grid management '''
    def __init__(self, xml_node):
        self.rows = xml_node.attributes['rows'].value
        self.columns = xml_node.attributes['columns'].value
        self.cf_list=[]
        for cf in xml_node.childNodes:
            if cf.nodeType != cf.ELEMENT_NODE or cf.nodeName != "configure":
                continue
            if "row" in cf.attributes.keys():
                cf_func = "rowconfigure"
                n = cf.attributes["row"].value
            elif "column" in cf.attributes.keys():
                cf_func = "columnconfigure"
                n = cf.attributes["column"].value
            else:
                continue
            self.cf_list.append((cf_func, n, cf.attributes["weight"].value))

class GridLocation:
    ''' implement grid_location in interface xml '''
    def __init__(self, xml_node):
        self.dict={}
        for a in xml_node.attributes.values():
            self.dict[a.name] = a.value

class Widget:
    ''' implement widget in xml '''
    def __init__(self, xml_node):
        if 'name' in xml_node.attributes.keys():
            self.name = xml_node.attributes["name"].value

        self.tp   = xml_node.attributes["type"].value
        self.display = True
        if 'display' in xml_node.attributes.keys():
            self.display = xml_node.attributes['display'].value == 'yes'

        self.attr = {}
        self.widgets=[]
        self.bindings = []
        for node in xml_node.childNodes:
            if node.nodeType == node.ELEMENT_NODE:
                if node.nodeName == "grid_management":
                    self.grid_management = GridManagement(node)
                elif node.nodeName == "grid_location":
                    self.grid_location = GridLocation(node)
                elif node.nodeName == "widget_attribute":
                    for a in node.attributes.values():
                      self.attr[a.name] = a.value  
                elif node.nodeName == "widget":
                    self.widgets.append(Widget(node))
                elif node.nodeName == 'binding':
                    seq = node.attributes["sequence"].value
                    self.bindings.append((seq, \
                                         node.attributes["function"].value))

class Text:
    dict = {}
    def __init__(self, xml_node):
        for n in xml_node.childNodes:
            if n.nodeType == n.ELEMENT_NODE:
                if n.nodeName == 'English': self.english = n.firstChild.data
                elif n.nodeName == 'Chinese': self.chinese = n.firstChild.data
        if 'key' in xml_node.attributes.keys():
            self.key = xml_node.attributes['key'].value
        else:
            self.key = self.english
        Text.dict[self.key.lower()] = self

class Root:
    ''' tk root '''
    def __init__(self, xml_root):
        self.width = xml_root.attributes['width'].value
        self.height = xml_root.attributes['height'].value
        self.leftx  = xml_root.attributes['leftx'].value
        self.uppery = xml_root.attributes['uppery'].value

        self.widgets = []
        for n in xml_root.childNodes:
            if n.nodeType == n.ELEMENT_NODE:
                if n.nodeName == 'widget':
                    w = Widget(n)
                    self.widgets.append(w)
                elif n.nodeName == 'grid_management': self.grid_management = GridManagement(n)
                elif n.nodeName == "text": Text(n)

def translate(txt, lang):
    k = txt.lower()
    if k in Text.dict.keys() and (txt[0] == '#' or lang != 'english'):
        return getattr(Text.dict[k], lang)
    else:
        return txt
